package logic

import (
	"C/renrenzhuC_srv/model/mysql"
	"C/renrenzhuC_srv/model/redis"
	"context"
	"fmt"

	"C/renrenzhuC_srv/internal/svc"
	"C/renrenzhuC_srv/renrenzhuC"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *renrenzhuC.UserLoginRequest) (*renrenzhuC.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get(context.Background(), "Sms:login"+in.Tel)
	if get.Val() != in.Send {
		logx.Errorf("验证码错误")
		return nil, fmt.Errorf("验证码错误")
	}
	var finduser mysql.User
	err := finduser.Find(in.Tel)
	if err != nil {
		logx.Errorf("没找到用户")
		return nil, fmt.Errorf("没找到该用户")
	}
	if finduser.Password != in.Password {
		logx.Errorf("密码错误")
		return nil, fmt.Errorf("密码错误")
	}
	redis.Client.Del(context.Background(), "Sms:login"+in.Tel)
	return &renrenzhuC.UserLoginResponse{UserId: int64(finduser.ID)}, nil
}
